<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="模型编号" prop="mxuuid">
        <el-input
          v-model="queryParams.mxuuid"
          placeholder="请输入模型编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编码" prop="flBm">
        <el-input
          v-model="queryParams.flBm"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级编码ID" prop="sjId">
        <el-input
          v-model="queryParams.sjId"
          placeholder="请输入上级编码ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="删除标志" prop="bsblag">
        <el-input
          v-model="queryParams.bsblag"
          placeholder="请输入删除标志"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ysgj:sjxwgx:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="sjxwgxList"
      row-key="flBm"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="模型编号" align="center" prop="mxuuid" />
      <el-table-column label="分类标识,1，目录，2-风险点，3-风险点关联数据,4-独立数据" align="center" prop="flbsDm" />
      <el-table-column label="关联数据类型:1-指标，2-元数据" align="center" prop="glsjLx" />
      <el-table-column label="编码" align="center" prop="flBm" />
      <el-table-column label="上级编码：根为0" align="center" prop="sjflBm" />
      <el-table-column label="上级编码ID" align="center" prop="sjId" />
      <el-table-column label="序号" align="center" prop="xh" />
      <el-table-column label="规则差异标记，0-无差异，1-有差异" align="center" prop="gzcyBj" />
      <el-table-column label="得分说明(规则说明)" align="center" prop="gzsm" />
      <el-table-column label="权重" align="center" prop="qz" />
      <el-table-column label="权重调整" align="center" prop="qztz" />
      <el-table-column label="1-存储过程，2-函数，3-程序，4-xml" align="center" prop="qzgzwjLx" />
      <el-table-column label="权重规则文件" align="center" prop="qzgzwj" />
      <el-table-column label="参考标准值" align="center" prop="ckbzz" />
      <el-table-column label="1-存储过程，2-函数，3-程序，4-xml" align="center" prop="gzwjLx" />
      <el-table-column label="规则文件" align="center" prop="gzwj" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="modifiDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifiDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="删除标志" align="center" prop="bsblag" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ysgj:sjxwgx:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ysgj:sjxwgx:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改数据分类模型关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模型编号" prop="mxuuid">
          <el-input v-model="form.mxuuid" placeholder="请输入模型编号" />
        </el-form-item>
        <el-form-item label="分类标识,1，目录，2-风险点，3-风险点关联数据,4-独立数据" prop="flbsDm">
          <el-input v-model="form.flbsDm" placeholder="请输入分类标识,1，目录，2-风险点，3-风险点关联数据,4-独立数据" />
        </el-form-item>
        <el-form-item label="关联数据类型:1-指标，2-元数据" prop="glsjLx">
          <el-input v-model="form.glsjLx" placeholder="请输入关联数据类型:1-指标，2-元数据" />
        </el-form-item>
        <el-form-item label="编码" prop="flBm">
          <el-input v-model="form.flBm" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="上级编码：根为0" prop="sjflBm">
          <treeselect v-model="form.sjflBm" :options="sjxwgxOptions" :normalizer="normalizer" placeholder="请选择上级编码：根为0" />
        </el-form-item>
        <el-form-item label="上级编码ID" prop="sjId">
          <el-input v-model="form.sjId" placeholder="请输入上级编码ID" />
        </el-form-item>
        <el-form-item label="序号" prop="xh">
          <el-input v-model="form.xh" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="规则差异标记，0-无差异，1-有差异" prop="gzcyBj">
          <el-input v-model="form.gzcyBj" placeholder="请输入规则差异标记，0-无差异，1-有差异" />
        </el-form-item>
        <el-form-item label="得分说明(规则说明)" prop="gzsm">
          <el-input v-model="form.gzsm" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="权重" prop="qz">
          <el-input v-model="form.qz" placeholder="请输入权重" />
        </el-form-item>
        <el-form-item label="权重调整" prop="qztz">
          <el-input v-model="form.qztz" placeholder="请输入权重调整" />
        </el-form-item>
        <el-form-item label="1-存储过程，2-函数，3-程序，4-xml" prop="qzgzwjLx">
          <el-input v-model="form.qzgzwjLx" placeholder="请输入1-存储过程，2-函数，3-程序，4-xml" />
        </el-form-item>
        <el-form-item label="权重规则文件" prop="qzgzwj">
          <el-input v-model="form.qzgzwj" placeholder="请输入权重规则文件" />
        </el-form-item>
        <el-form-item label="参考标准值" prop="ckbzz">
          <el-input v-model="form.ckbzz" placeholder="请输入参考标准值" />
        </el-form-item>
        <el-form-item label="1-存储过程，2-函数，3-程序，4-xml" prop="gzwjLx">
          <el-input v-model="form.gzwjLx" placeholder="请输入1-存储过程，2-函数，3-程序，4-xml" />
        </el-form-item>
        <el-form-item label="规则文件" prop="gzwj">
          <el-input v-model="form.gzwj" placeholder="请输入规则文件" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改时间" prop="modifiDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.modifiDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志" prop="bsblag">
          <el-input v-model="form.bsblag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSjxwgx, getSjxwgx, delSjxwgx, addSjxwgx, updateSjxwgx, exportSjxwgx } from "@/api/ysgj/sjxwgx";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Sjxwgx",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 数据分类模型关系表格数据
      sjxwgxList: [],
      // 数据分类模型关系树选项
      sjxwgxOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        mxuuid: undefined,
        flBm: undefined,
        sjId: undefined,
        createDate: undefined,
        bsblag: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据分类模型关系列表 */
    getList() {
      this.loading = true;
      listSjxwgx(this.queryParams).then(response => {
        this.sjxwgxList = this.handleTree(response.data, "flBm", "sjflBm");
        this.loading = false;
      });
    },
    /** 转换数据分类模型关系数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.flBm,
        label: node.flBm,
        children: node.children
      };
    },
	/** 查询部门下拉树结构 */
    getTreeselect() {
      listSjxwgx().then(response => {
        this.sjxwgxOptions = [];
        const data = { flBm: 0, flBm: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "flBm", "sjflBm");
        this.sjxwgxOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        mxuuid: undefined,
        flbsDm: undefined,
        glsjLx: undefined,
        flBm: undefined,
        sjflBm: undefined,
        sjId: undefined,
        xh: undefined,
        gzcyBj: undefined,
        gzsm: undefined,
        qz: undefined,
        qztz: undefined,
        qzgzwjLx: undefined,
        qzgzwj: undefined,
        ckbzz: undefined,
        gzwjLx: undefined,
        gzwj: undefined,
        createDate: undefined,
        modifiDate: undefined,
        bsblag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
	  this.getTreeselect();
      this.open = true;
      this.title = "添加数据分类模型关系";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
	  this.getTreeselect();
      if (row != undefined) {
        this.form.sjflBm = row.flBm;
      }
      getSjxwgx(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据分类模型关系";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSjxwgx(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addSjxwgx(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除数据分类模型关系编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSjxwgx(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>